package com.chap6;

import java.util.concurrent.Exchanger;

/**
 * <p>
 * <code>ExchangerDemo</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-03-20 9:30
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class ExchangerDemo {
    private static Exchanger exchanger = new Exchanger();
    static String goods = "电脑";
    static String money = "$4000";


    public static void main(String[] args) throws InterruptedException {
        System.out.println("准备交易，一手交钱一手交货...");

                    //卖家
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("卖家到了，已经准备好货:"+goods);
                    //等待另一个线程，把goods给他，接收他给的money
                    String money = (String) exchanger.exchange(goods);
                    System.out.println("卖家收到钱："+money);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        Thread.sleep(3000);

        //买家
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("买家到了，已经准备好钱："+money);
                try {
                    String goods = (String) exchanger.exchange(money);
                    System.out.println("买家收到货："+goods);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
